package com.example.java_chatroom.api;

import com.example.java_chatroom.model.Message;
import com.example.java_chatroom.model.MessageMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author Q
 * @create 2025-07-07-16:08
 */
@RestController
public class MessageAPI {
    @Resource
    private MessageMapper messageMapper;

    /**
     * 获取历史消息
     * 降序获取100条消息，注意在前端升序显示消息
     * @param sessionId
     * @return
     */
    @GetMapping("/message")
    public Object getMessage(int sessionId) {
        List<Message> messages = messageMapper.getMessageBySessionId(sessionId);
        Collections.reverse(messages);
        return messages;
    }
}
